from common import BaseRequestHandler
from model import *
import logging


class StoryBaseHandler(BaseRequestHandler):
    def _post(self):
        story_id = self.request.get('story_id')
        self._story = Story.get_by_id(int(story_id))
        if self.is_user_allowed():
            self._post_()
            self.json_response(self.get_response())
        else:
            response = 'Not allowed!!!'
            self.response.set_status(500)
            self.json_response(response)

    def is_user_allowed(self):
        return self._story.contains_current_user() and self._story.status != 'Closed'

class TimeWorkedForm(StoryBaseHandler):
    def _post_(self):
        self._time_worked = StoryTimeWorked()
        self._time_worked.story = self._story
        self._time_worked.time = float(self.request.get('time'))
        self._time_worked.put()
        self.event = TimeEvent.create(self._story, self._time_worked.time, self.request.get('description'))
    def is_user_allowed(self):
        return self._story.current_user_assigned() and self._story.status != 'Closed'
    def get_response(self):
        html = None
        if self.request.get('html') != 'False':
            html = self.generate('event.html', {'event': self.event})
        elapsed_time = self._story.elapsed_time()
        return {    'elapsed_time': elapsed_time,
                    'remaining_time': self._story.estimated_time - elapsed_time,
                    'percentage': self._story.percentage(),
                    'html': html}

class AssignForm(StoryBaseHandler):
    def _post_(self):
        member_id = self.request.get('assigned_to')
        member = Member.get_by_id(int(member_id))
        self.event = AssignEvent.create(self._story, member.user, self._story.assigned_to, self.request.get('description'))
        self._story.assigned_to = member.user
        self._story.put()
    def get_response(self):
        html = None
        if self.request.get('html') != 'False':
            html = self.generate('event.html', {'event': self.event})
        current_user = self._story.assigned_to == users.get_current_user()
        return {'assigned_to': self._story.assigned_to.nickname(),
                'current_user': current_user,
                'status':self._story.status, 'html': html}

class ChangeStoryStatus(StoryBaseHandler):
    def _post_(self):
        new_status = self.request.get('status')
        old_status = self._story.status
        self._story.status = new_status
        self._story.put()
        self.event = ResolveEvent.create(self._story, new_status, old_status,
                            self.request.get('description'))
    def is_user_allowed(self):
        return self._story.current_user_assigned() and self._story.status != 'Closed'
    def get_response(self):
        html = None
        if self.request.get('html') != 'False':
            html = self.generate('event.html', {'event': self.event})
        return {'status':self._story.status, 'html': html}

class ReopenStory(StoryBaseHandler):
    def _post_(self):
        new_status = 'Active'
        old_status = self._story.status
        self._story.status = new_status
        self._story.put()
        self.event = ResolveEvent.create(self._story, new_status, old_status,
                            self.request.get('description'))
    def is_user_allowed(self):
        return self._story.contains_current_user()
    def get_response(self):
        html = None
        if self.request.get('html') != 'False':
            html = self.generate('event.html', {'event': self.event})
        return {'status':self._story.status, 'html': html}

class PostponeStoryHandler(StoryBaseHandler):
    def _post_(self):
        backlog = self.request.get('backlog')
        self.backlog_ = StoryContainer.get(backlog)
        self.event = MoveEvent.create(self._story, self._story.story_container, self.backlog_,
                         self.request.get('description'))
        self._story.story_container = self.backlog_
        self._story.put()
        
    def get_response(self):
        html = None
        if self.request.get('html') != 'False':
            html = self.generate('event.html', {'event': self.event})
        return {'name': self.backlog_.name, 'id': self.backlog_.key().id(), 'html': html}

class EditStoryDescription(StoryBaseHandler):
    def _post_(self):
        description = self.request.get('description')
        self.event = EditDescEvent.create(self._story, self._story.description, description)
        self._story.description = description
        self._story.put()
    def get_response(self):
        html = None
        if self.request.get('html') != 'False':
            html = self.generate('event.html', {'event': self.event})
        return {'description': self._story.description, 'html': html}

class CommentStory(StoryBaseHandler):
    def _post_(self):
        comment = self.request.get('comment')
        self.event = CommentEvent.create(self._story, comment)
    def is_user_allowed(self):
        return self._story.status != 'Closed'
    def get_response(self):
        html = None
        if self.request.get('html') != 'False':
            html = self.generate('event.html', {'event': self.event})
        return {'status':self._story.status, 'html': html}